<template>
  <div>
    <el-row>
      <el-col style="margin-top: 30px">
        <el-select
          v-model="Searchinfo.status"
          placeholder="选择试剂状态"
          size="large"
          style="width: 20%"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <el-input
          v-model="Searchinfo.devicenum"
          placeholder="请输入试剂编号"
          clearable
          style="width: 20%; margin-left: 2%"
        />

        <el-input
          v-model="Searchinfo.distributorname"
          placeholder="请输入关联推销员名称"
          clearable
          style="width: 20%; margin-left: 2%"
        />
        <el-button
          type="primary"
          :icon="Search"
          style="margin-left: 2%"
          @click="GetList()"
          >搜索</el-button
        >
      </el-col>

      <el-col style="margin-top: 10px">
        <el-table
          class="box-table"
          :data="Data"
          :stripe="true"
          :border="true"
          :fit="true"
          v-if="Data != null"
        >
          <el-table-column
            prop="devicenum"
            label="试剂编号"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="createdat"
            label="入库时间"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            prop="status"
            label="试剂状态"
            width="auto"
            align="center"
            header-align="center"
          >
            <template #default="scope">
              <p v-if="scope.row.status == '未用'" style="color: green">未用</p>
              <p v-if="scope.row.status == '已用'" style="color: grey">已用</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="distributorname"
            label="关联推销员名称"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="auto"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="delBerbak(scope.row)"
                style="color: red"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <el-col style="margin: 0 auto">
        <el-card>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :total="Total"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRaw, onMounted, onBeforeMount } from "vue";
import apiService from "@/services/apiService";
import { v4 as uuidv4 } from "uuid";
import { Search } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { pagedDistributorDevices, deleteDistributorDevices } from "@/api/api";
defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: "Outcome"
});

//表格数据
const Data = ref<any>([]);
const currentPage = ref(1); // 当前页数
const pageSize = ref(10); // 每页条数
const Total = ref<number>(1); //总条数
const reply = ref<number>(1); //当前显示
const Searchinfo = ref<any>({
  status: null,
  devicenum: "",
  distributorname: ""
});
const options = [
  {
    value: "已用",
    label: "已用"
  },
  {
    value: "未用",
    label: "未用"
  },
  {
    value: null,
    label: "全部"
  }
];
const del_arr = ref([]);

//获取未回复表单数据
const GetList = () => {
  Data.value = [];
  const data = ref({
    devicenum: Searchinfo.value.devicenum,
    status: Searchinfo.value.status,
    distributorname: Searchinfo.value.distributorname,
    pagenumber: currentPage.value,
    pagesize: pageSize.value
  });
  pagedDistributorDevices(data.value)
    .then((res: any) => {
      console.log("表单数据", res);
      Data.value.push(...res.data);
      Total.value = res.total;
    })
    .catch((err: any) => {
      console.log(err);
    });
};

// 监听页码变化
const handleCurrentChange = val => {
  currentPage.value = val;
  GetList();
};

// 监听每页条数变化
const handleSizeChange = val => {
  pageSize.value = val;
};

//删除
const delBerbak = (row: any) => {
  del_arr.value.push(row.id);
  ElMessageBox.confirm("确认删除?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      // bindDistributorDevices
      const data = del_arr.value;
      deleteDistributorDevices(data)
        .then((res: any) => {
          if (res.code == 200) {
            ElMessage({
              type: "success",
              message: "删除成功"
            });
            del_arr.value = [];
            GetList();
          }
        })
        .catch((err: any) => {
          console.log(err);
        });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除"
      });
    });
};

//关闭弹窗
const handleClose = (done: () => void) => {
  // ElMessageBox.confirm("确认关闭?")
  //   .then(() => {
  done();
  // })
  // .catch(() => {
  //   // catch error
  // });
};

onMounted(() => {
  GetList();
});
</script>

<style lang="scss" scoped>
.active-button {
  background-color: #409eff;
  /* 设置按钮激活时的背景颜色 */
  color: white;
  /* 设置按钮激活时的文字颜色 */
}

:deep(.el-select--large .el-select__wrapper) {
  min-height: 0;
}
</style>
